﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Data.Entity;
using Gqq.CodeFirst.Palace;
using Gqq.CodeFirst.ProcessPalace;
using Gqq.Common.Helper;
using Gqq.Common.Helper.CodeFirstLog;

namespace Gqq.CodeFirst
{
    internal class Program
    {
        private static void Main(string[] args)
        {

            //Initilize();
//            Process.DoTest();
//            Part7.TestTrackEntities();
//            Part8.TestProxyDisabled();
//            Part10.TestRawSqls();
//            Part8.DoTest();
//            Part7.DoTest();
            Part6.DoTest();
            PalaceCommon.GetMaxPrincessId();
            Console.WriteLine("完成！");
            Console.ReadKey();
        }

// ReSharper disable UnusedMember.Local
        static void Initilize()
// ReSharper restore UnusedMember.Local
        {
            using (var db = new UnicornsContext())
            {
                var princesses = db.Princesses.ToList();

                if (princesses.Count > 0)
                    princesses.ForEach(x => CodeLogManager.DLog.InfoFormat("Princess:{0}——{1}", x.Id, x.Name));
                else
                    CodeLogManager.DLog.Info("princess表中数据数据为空");

                //                try
                //                {
                var cinderella = new Princess { Name = "Cinderella" };
                var sleepingBeauty = new Princess { Name = "Sleeping Beauty" };
                var snowWhite = new Princess { Name = "Snow White" };


                // 特别注意，不用手动add，在下一步添加Unicorn时，会自动把这几个Princess加进去。
                //db.Princesses.Add(cinderella);
                //db.Princesses.Add(sleepingBeauty);
                //db.Princesses.Add(snowWhite);

                new List<Unicorn>
                    {
                        new Unicorn {Name = "Binky", Princess = cinderella},
                        new Unicorn {Name = "Silly", Princess = cinderella},
                        new Unicorn {Name = "Beepy", Princess = sleepingBeauty},
                        new Unicorn {Name = "Creepy", Princess = snowWhite}
                    }.ForEach(u => db.Unicorns.Add(u));

                var efCastle = new Castle
                {
                    Name = "The EF Castle",
                    Location = new Location
                    {
                        City = "Redmond",
                        Kingdom = "Rainier",
                        ImaginaryWorld = new ImaginaryWorld
                        {
                            Name = "Magic Unicorn World",
                            Creator = "ADO.NET"
                        }
                    },
                };

                new List<LadyInWaiting>
                    {
                        new LadyInWaiting
                            {
                                Princess = cinderella,
                                Castle = efCastle,
                                FirstName = "Lettice",
                                Title = "Countess"
                            },
                        new LadyInWaiting
                            {
                                Princess = sleepingBeauty,
                                Castle = efCastle,
                                FirstName = "Ulrika",
                                Title = "Lady"
                            },
                        new LadyInWaiting
                            {
                                Princess = snowWhite,
                                Castle = efCastle,
                                FirstName = "Yolande",
                                Title = "Duchess"
                            }
                    }.ForEach(l => db.LadiesInWaiting.Add(l));
                db.SaveChanges();


                //}
                //catch(Exception ex)
                //{
                //    CodeLogManager.DLog.InfoFormat("错误消息：{0}——错误地方：{1}", ex.Message, ex.StackTrace);
                //}
            }
        }

    }
}

